package com.dxy.md.service;

import com.dxy.md.entity.User;
import com.dxy.md.mapper.master.MasterUserMapper;
import com.dxy.md.mapper.slave.SlaveUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MasterUserMapper masterUserMapper;
    @Autowired
    private SlaveUserMapper slaveUserMapper;

    @Override
    public void saveUser(User user) {
        masterUserMapper.insert(user);
    }

    @Override
    public User findById(Long id) {
        return slaveUserMapper.selectById(id);
    }

    @Override
    @Transactional // 默认会使用标记了@Primary的masterTransactionManager
//    @Transactional(transactionManager = "slaveTransactionManager")
    public void testTransaction() {
        User user = new User().setName("xxx");
        masterUserMapper.insert(user);
//        slaveUserMapper.insert(user);
        throw new RuntimeException("测试事务报错");
    }

    @Override
    public void deleteUser(Long id) {
//        masterUserMapper.deleteUser(id);
        slaveUserMapper.deleteUser(id);
    }
}
